<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MessagePushController extends Controller
{
    public function message(Request $request)
    {
        $this->validate($request, [
            'msg' => 'string|required',
            'type' => 'string|required|in:single,broadcast,group',
            'fd' => 'nullable|required_if:type,single|integer',
            'groupId' => 'nullable|required_if:type,group|integer',
        ]);

        $client = new Client(['base_uri' => 'http://127.0.0.1:'.config('websocket.port')]);
        $response = $client->request('POST', '/post', [
            'form_params' => $request->all()
        ]);

        return new JsonResponse(['status' => $response->getStatusCode() == 200 ? 'success' : 'fail']);
    }

}
